package com.oa.admin.service.impl;

import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.OutApplyAddDTO;
import com.oa.admin.dto.OutApplyListDTO;
import com.oa.admin.service.IOutApplyFileService;
import com.oa.admin.service.IOutApplyPersonnelService;
import com.oa.admin.vo.OutApplyDetailVO;
import com.oa.admin.vo.OutApplyListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.OutApply;
import com.oa.admin.mapper.OutApplyMapper;
import com.oa.admin.service.IOutApplyService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.entity.User;
import com.oa.system.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 外出出差申请表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class OutApplyServiceImpl extends ServiceImpl<OutApplyMapper, OutApply> implements IOutApplyService {

    private final OutApplyMapper outApplyMapper;

    @Autowired
    private IOutApplyPersonnelService outApplyPersonnelService;

    @Autowired
    private IOutApplyFileService outApplyFileService;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    private IUserService userService;

    @Autowired
    public OutApplyServiceImpl(OutApplyMapper outApplyMapper) {
        this.outApplyMapper = outApplyMapper;
    }

    @Override
    public PageResult<OutApplyListVO> findOutApplyList(OutApplyListDTO outApplyListDTO) {
        return new PageResult<>(outApplyMapper.findOutApplyList(outApplyListDTO.getPage(), outApplyListDTO));
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp addOutApply(OutApplyAddDTO outApplyAddDTO) throws BusinessException, DataIsEmptyException {

        if(outApplyAddDTO.getOutApply().getStartTime().getTime() >= outApplyAddDTO.getOutApply().getEndTime().getTime()){
            return new Resp().failure("结束时间不能大于等于开始时间");
        }

        outApplyAddDTO.getOutApply().setType(outApplyAddDTO.getType());

        if(outApplyMapper.insert(outApplyAddDTO.getOutApply()) > 0){
            outApplyAddDTO.getOutApplyPersonnels().forEach(d -> d.setOutApplyId(outApplyAddDTO.getOutApply().getId()));
            outApplyAddDTO.getOutApplyFile().setOutApplyId(outApplyAddDTO.getOutApply().getId());

            if(outApplyAddDTO.getOutApplyFile() != null && StringUtils.isNotEmpty(outApplyAddDTO.getOutApplyFile().getFileAddress())){
                outApplyFileService.insert(outApplyAddDTO.getOutApplyFile());
            }

            if(outApplyPersonnelService.insertBatch(outApplyAddDTO.getOutApplyPersonnels())){
                String applyApprovalName = "";
                String processId = "";
                if(outApplyAddDTO.getType() == 2){
                    applyApprovalName = ProcessDefineEnum.BUSINESS_TRIP.getText();
                    processId = ProcessDefineEnum.BUSINESS_TRIP.getId();
                }else{
                    outApplyAddDTO.setType(3);
                    applyApprovalName = ProcessDefineEnum.GO_OUT.getText();
                    processId = ProcessDefineEnum.GO_OUT.getId();
                }
                User user = userService.selectById(outApplyAddDTO.getOutApply().getProposer());

                ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
                applyApprovalAddDTO.setApplyId(outApplyAddDTO.getOutApply().getId());
                applyApprovalAddDTO.setApprovalProcessId(processId);
                applyApprovalAddDTO.setApplyApprovalName(user.getName()+applyApprovalName);
                applyApprovalAddDTO.setApprovalUserId(outApplyAddDTO.getOutApply().getProposer());
                return applyApprovalService.addApproval(applyApprovalAddDTO);
            }
        }
        throw new BusinessException("添加申请失败!");
    }

    @Override
    public OutApplyDetailVO findOutApplyDetailById(String id) {
        return outApplyMapper.findOutApplyDetailById(id);
    }

    @Override
    public void export(OutApplyListDTO outApplyListDTO, HttpServletResponse response) {
        List<OutApplyListVO> outApplyListVOS;
        if(outApplyListDTO.getPage() != null){
            outApplyListVOS = outApplyMapper.findOutApplyList(outApplyListDTO.getPage(), outApplyListDTO);
        }else{
            outApplyListVOS = outApplyMapper.findOutApplyList(new Page(), outApplyListDTO);
        }

        String[] rows = {"id", "审核ID", "申请时间", "开始时间", "结束时间", "外出地点", "外出事由", "部门", "申请人", "预估费用", "审核状态", "备注"};

        if(outApplyListDTO.getType().equals("2")){
            try {
                ExcelUtil.downloadExcel("出差申请" + DateUtil.getDayMonth(), rows, outApplyListVOS, "出差申请" + DateUtil.getDayMonth()+".xls", response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            try {
                ExcelUtil.downloadExcel("外出申请" + DateUtil.getDayMonth(), rows, outApplyListVOS, "外出申请" + DateUtil.getDayMonth()+".xls", response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
